﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using System.IO;

namespace AzureDriveExplorer
{
    public partial class Loading : Form
    {
        const int DEFAULT_TIME = 1000;
        //Azure
        private static GetListVHD.AzureDriveExplorerServiceSoapClient list = new GetListVHD.AzureDriveExplorerServiceSoapClient();       
        public GetListVHD.ArrayOfString ListVHD=null;

        Thread t;
        public Loading()
        {
            InitializeComponent();
           
            this.Shown += new EventHandler(fSplashScreen_Shown);
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
            this.closeButton.Click += new EventHandler(closeButton_Click);
        }
        void closeButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        void fSplashScreen_Shown(object sender, EventArgs e)
        {
            initvalues();
            t = new System.Threading.Thread(initApplication);
            t.Start();
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null && t.IsAlive)
            {
                t.Abort();
                t = null;
            }
        }

        private void initvalues()
        {
            this.label1.Text = String.Format("{0} {1}", Application.ProductName, Application.ProductVersion);           
            this.label2.Text = "Initializing...";
          
            
        }

        private void initApplication()
        {
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => GetListAzure("Connectiong to Azure...")));
            Thread.Sleep(DEFAULT_TIME);
            this.Invoke((MethodInvoker)(() => setMessage("Loader...")));    
            Thread.Sleep(DEFAULT_TIME);
            if (this.InvokeRequired) this.Invoke(new Action(finishProcess));
        }

        private void setMessage(string msg)
        {
            label2.Text = msg;
            
        }

        private void GetListAzure(string msg)
        {
            label2.Text = msg;

            //GetListVHD.Service1SoapClient list = new GetListVHD.Service1SoapClient();
            //Azure

            //Local Azure
            //GetListVHDLocalAzure.ArrayOfString ListVHD;

            //Local
            //GetListVHDLocal.ArrayOfString listDirectory, listFiles, ListVHD;

            //Azure
            GetListVHD.ArrayOfString vecFileAccount = new GetListVHD.ArrayOfString();



            List<string> ListFileAccount = new List<string>();
            //read file account
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(Constant.FileConfig))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        vecFileAccount.Add(line);
                    }
                }

                //listDirectory = list.getDirectory();
                //listFiles = list.getFiles();
                ListVHD = list.getVHD(vecFileAccount, Constant.key());
            }
            catch (Exception ex)
            {

            }


        }

        private void GetListAzure1(string msg)
        {
            label2.Text = msg;
            
            //GetListVHD.Service1SoapClient list = new GetListVHD.Service1SoapClient();
            //Azure
            
            //Local Azure
            //GetListVHDLocalAzure.ArrayOfString ListVHD;

            //Local
            //GetListVHDLocal.ArrayOfString listDirectory, listFiles, ListVHD;

            //Azure
            GetListVHD.ArrayOfString vecFileAccount = new GetListVHD.ArrayOfString();
           

            List<string> ListFileAccount = new List<string>();
            //read file account
            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(Constant.FileConfig))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        vecFileAccount.Add(line);
                    }
                }

                //listDirectory = list.getDirectory();
                //listFiles = list.getFiles();
                ListVHD = list.getVHD(vecFileAccount, Constant.key());
            }
            catch (Exception ex)
            {
              
            }


        }

        private void finishProcess()
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

    }
}
